package array;

import java.util.ArrayList;

public class L45 {
    public static int jump(int[] nums) {
       if (nums.length == 1) return 0;
       int jumps = 0;
       int currentend = 0;
       int farthest = 0;
       for (int i = 0; i < nums.length - 1; i++) {
           //计算当前能跳跃的最远的距离
           farthest = Math.max(farthest, i + nums[i]);

           if (i == currentend) {
               jumps++;
               currentend = farthest;
           }
           if (currentend >= nums.length - 1) {
               break;
           }
       }
       return jumps;
    }
    public static void main(String[] args) {
        // 测试用例
        int[] nums = {2, 3, 1, 1, 4};
        System.out.println("最少跳跃次数: " + jump(nums));  // 输出应为 2

    }
}


